package com.jediflasher.game_server.data

import com.jediflasher.game_server.core.events.EventDispatcher
import com.jediflasher.game_server.events.data.world.{WorldActorDataEventType, WorldActorDataEvent}
import java.sql.ResultSet
import com.jediflasher.game_server.database.DataBaseManager

/**
 * User: Obi
 * Date: 05.08.11
 * Time: 17:53
 */

class UserData(val id: Int, private var result:ResultSet = null) extends EventDispatcher {

	var name: String = null;

	var x: Int = 0;

	var y: Int = 0;

	init();

	private def init() {
		if(this.result == null) {
			val query = "SELECT name FROM users WHERE id='"+id+"'";
			this.result = DataBaseManager.executeQuery(query)
		}
		this.name = this.result.getString("name");
		this.x = 120;
		this.y = 120;
	}

	def setPosition(x: Int, y: Int) {
		var positionChanged:Boolean = false;
		if(this.x != x) {
			this.x = x;
			positionChanged = true;
		}

		if(this.y != y) {
			this.y = y;
			positionChanged = true;
		}

		if (positionChanged) dispatchEvent(new WorldActorDataEvent(WorldActorDataEventType.POSITION_CHANGE))
	}
}